
package com.ami.kvm.jviewer.soc.reader;

import java.net.Socket;
import java.nio.ByteBuffer;

import com.ami.kvm.jviewer.gui.JViewerApp;
import com.ami.kvm.jviewer.jvvideo.JVVideo;
import com.ami.kvm.jviewer.kvmpkts.HeaderReader;
import com.ami.kvm.jviewer.kvmpkts.KVMClient;
import com.ami.kvm.jviewer.kvmpkts.KVMReader;
import com.ami.kvm.jviewer.soc.SOCApp;

public class HardwareCursorReader implements KVMReader {
	private KVMClient client;
	private byte[] hwCursorBuf;
	private ByteBuffer hwCursorByteBuf;
	private int hwCursorIndex;

	public HardwareCursorReader(KVMClient paramKVMClient, JVVideo paramJVVideo) {
		this.client = paramKVMClient;
		this.hwCursorBuf = new byte[8205];
		this.hwCursorIndex = 0;
		this.hwCursorByteBuf = ByteBuffer.wrap(this.hwCursorBuf);
	}

	public void initialize() {
		this.hwCursorByteBuf.position(0);
		this.hwCursorByteBuf.limit(this.client.m_pktHdr.pktSize);
		this.hwCursorIndex = 0;
	}

	public int read(Socket paramSocket) {
		try {
			byte[] arrayOfByte = new byte[this.hwCursorByteBuf.remaining()];
			this.hwCursorIndex = this.client.read_data(paramSocket, arrayOfByte);
			this.hwCursorByteBuf.put(arrayOfByte);
		} catch (Exception localException) {
			System.out.println("HW CURSOR " + localException);
			return -1;
		}
		HeaderReader localHeaderReader;
		if (this.hwCursorIndex == 0) {
			localHeaderReader = this.client.getHdrReader();
			localHeaderReader.initialize();
			this.client.setState(localHeaderReader);
			return 0;
		}

		if (this.hwCursorIndex == this.client.m_pktHdr.pktSize) {
			localHeaderReader = this.client.getHdrReader();
			localHeaderReader.initialize();
			this.client.setState(localHeaderReader);

			((SOCApp) JViewerApp.getInstance().getSoc_App()).onHardwareCursor(this.hwCursorByteBuf);
		}
		return 0;
	}
}